package controllers

import (
	"github.com/SebastiaanKlippert/go-wkhtmltopdf"
	"github.com/gin-gonic/gin"
	"log"
	"strings"
	"wakitsoft.com/resume/consts"
	"wakitsoft.com/resume/db"
	"wakitsoft.com/resume/dto"
	"wakitsoft.com/resume/errs"
	"wakitsoft.com/resume/libs"
	"wakitsoft.com/resume/models"
)

func GetResumes(c *gin.Context) {
	var resumes []models.Resume
	db.GetDB().Model(&models.Resume{}).Find(&resumes)
	libs.Success(c, resumes)
}

func GetResume(c *gin.Context) {
	id := c.Param("id")
	var resume models.Resume
	db.GetDB().Model(&models.Resume{}).
		Preload("Media").
		Preload("TechStacks").
		Preload("Educations").
		Preload("WorkExperiences").
		Preload("Projects").
		Where("id = ?", id).First(&resume)
	libs.Success(c, resume)
}

type createResumeRequest struct {
	Title string `json:"title" binding:"required"`
}

func CreateResume(c *gin.Context) {
	var req createResumeRequest
	if !libs.ValidateRequest(c, &req) {
		return
	}
	var resume = models.Resume{
		Title: req.Title,
	}
	db.GetDB().Model(&models.Resume{}).Create(&resume)
	libs.Success(c, resume)
}

func DeleteResume(c *gin.Context) {
	id := c.Param("id")
	var resume models.Resume
	db.GetDB().Model(&models.Resume{}).Where("id = ?", id).Find(&resume)
	if resume.ID > 0 {
		db.GetDB().Delete(&resume)
	}
	libs.Success(c, resume)
}

func UpdateResume(c *gin.Context) {
	var req dto.UpdateResumeRequest
	if !libs.ValidateRequest(c, &req) {
		return
	}
	id := c.Param("id")
	var resume models.Resume
	db.GetDB().Model(&models.Resume{}).Where("id = ?", id).First(&resume)
	if resume.ID == 0 {
		libs.Error(c, errs.ResourceNotFound, "未找到")
		return
	}
	if err := SyncResume(resume, req); err != nil {
		log.Println(err)
		return
	}
	db.GetDB().Model(&models.Resume{}).Where("id = ?", id).First(&resume)
	libs.Success(c, resume)
}

func SyncResume(resume models.Resume, req dto.UpdateResumeRequest) error {
	var res = req.Resume
	if res.Name != "" {
		resume.Name = res.Name
	}

	if res.Avatar != "" {
		resume.Avatar = res.Avatar
	}

	if res.Website != "" {
		resume.Website = res.Website
	}

	if res.Wechat != "" {
		resume.Wechat = res.Wechat
	}

	if res.Phone != "" {
		resume.Phone = res.Phone
	}

	if res.CSDN != "" {
		resume.CSDN = res.CSDN
	}

	if res.Github != "" {
		resume.Github = res.Github
	}

	if res.Email != "" {
		resume.Email = res.Email
	}

	if res.Description != "" {
		resume.Description = res.Description
	}
	log.Println(resume)
	tx := db.GetDB().Save(&resume)
	return tx.Error
}

type htmlRequest struct {
	Html string `json:"html" binding:"required"`
}

func CreatePdf(c *gin.Context) {
	var req htmlRequest
	if !libs.ValidateRequest(c, &req) {
		return
	}
	data := strings.Replace(consts.PdfTemplate, "{{CONTENT}}", req.Html, 1)

	pdfG, err := wkhtmltopdf.NewPDFGenerator()
	if err != nil {
		libs.Error(c, errs.InternalError, err.Error())
		return
	}
	libs.WriteToHTML("index.html", data)
	pdfG.Dpi.Set(300)
	pdfG.Orientation.Set(wkhtmltopdf.OrientationPortrait)
	//page := wkhtmltopdf.NewPageReader(strings.NewReader(data))
	//pdfG.AddPage(page)
	log.Println("going to baidu.com")
	pdfG.AddPage(wkhtmltopdf.NewPage("https://baidu.com"))
	if err := pdfG.Create(); err != nil {
		libs.Error(c, errs.InternalError, err.Error())
		return
	}
	if err := pdfG.WriteFile("resume.pdf"); err != nil {
		log.Println(err.Error())
		libs.Error(c, errs.InternalError, err.Error())
		return
	}
	libs.Success(c, data)
}
